package com.digitalchina.lxnsy.activity.old;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.OptionsPickerView;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessFile;
import com.digitalchina.lxnsy.api.BusinessShareFarm;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.ActivityTypeVO;
import com.digitalchina.lxnsy.entity.FileVO;
import com.digitalchina.lxnsy.entity.ShareBasicDataVO;
import com.digitalchina.lxnsy.entity.ShareFarmListVO;
import com.digitalchina.lxnsy.entity.ShareSelectCityVO;
import com.digitalchina.lxnsy.loader.GlideImageLoader;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.widget.EditTextWithClear;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/8/20 14:37
 * 修改备注：
 * ================================================
 */
public class AddShareFarmActivity extends BaseActivity {
    @Bind(R.id.et_name)
    EditTextWithClear etName;
    @Bind(R.id.tv_type)
    TextView tvType;
    @Bind(R.id.cb_share)
    CheckBox cbShare;
    @Bind(R.id.cb_sell)
    CheckBox cbSell;
    @Bind(R.id.et_sell_price)
    EditTextWithClear etSellPrice;
    @Bind(R.id.et_share_price)
    EditTextWithClear etSharePrice;
    @Bind(R.id.sp_type)
    Spinner spType;
    @Bind(R.id.et_cash_eposit)
    EditTextWithClear etCashEposit;
    @Bind(R.id.et_share_num)
    EditTextWithClear etShareNum;
    @Bind(R.id.cb_express)
    CheckBox cbExpress;
    @Bind(R.id.cb_take)
    CheckBox cbTake;
    @Bind(R.id.et_take_address)
    EditTextWithClear etTakeAddress;
    @Bind(R.id.rb_postage_no)
    RadioButton rbPostageNo;
    @Bind(R.id.rb_postage_yes)
    RadioButton rbPostageYes;
    @Bind(R.id.rg_postage)
    RadioGroup rgPostage;
    @Bind(R.id.et_post_price)
    EditTextWithClear etPostPrice;
    @Bind(R.id.iv_img)
    SimpleDraweeView ivImg;
    @Bind(R.id.et_info)
    EditText etInfo;
    @Bind(R.id.sdv_img0)
    SimpleDraweeView sdvImg0;
    @Bind(R.id.sdv_img1)
    SimpleDraweeView sdvImg1;
    @Bind(R.id.sdv_img2)
    SimpleDraweeView sdvImg2;
    @Bind(R.id.sdv_img3)
    SimpleDraweeView sdvImg3;
    @Bind(R.id.sdv_img4)
    SimpleDraweeView sdvImg4;
    @Bind(R.id.sdv_img5)
    SimpleDraweeView sdvImg5;
    @Bind(R.id.sdv_img6)
    SimpleDraweeView sdvImg6;
    @Bind(R.id.sdv_img7)
    SimpleDraweeView sdvImg7;
    @Bind(R.id.btn_save)
    Button btnSave;
    @Bind(R.id.layout_sell)
    LinearLayout layoutSell;
    @Bind(R.id.layout_share)
    LinearLayout layoutShare;
    @Bind(R.id.tv_scope)
    TextView tvScope;
    @Bind(R.id.layout_share_num)
    LinearLayout layoutShareNum;
    @Bind(R.id.layout_cash)
    LinearLayout layoutCash;
    @Bind(R.id.layout_address)
    LinearLayout layoutAddress;
    @Bind(R.id.rb_scope_all)
    RadioButton rbScopeAll;
    @Bind(R.id.rb_scope_select)
    RadioButton rbScopeSelect;
    @Bind(R.id.rg_scope)
    RadioGroup rgScope;


    private String productId = ""; // 共享产品id
    private String name = ""; // 共享名称
    private String type = ""; //  共享分类code
    private String shareType = ""; // 共享方式
    private String sharePrice = ""; // 共享价格
    private String salePrice = ""; // 出售价格
    private String unit = ""; // 共享价格单位code
    private String securityDeposit = ""; // 保证金
    private String productNum = ""; // 共享数量
    private String deliveryMode = ""; // 取送方式
    private String retentionAddress = ""; // 自提地址
    private String isFreePostage = "1"; // 运费设置 1-包邮  0设置邮费  默认包邮
    private String postage = ""; // 邮费
    private String shareScopeFlag = "1"; // 共享范围类型 1-不限 0-限制范围
    private String agricultureProductShareScopes = ""; // 范围类型
    private String mainImage = ""; // 主图
    private String introduce = ""; // 详情
    private String pictures = ""; //  详情图片(以逗号隔开)


    private List<String> emptyList = new ArrayList<>();
    private List<ShareBasicDataVO.TypeListBean> typeList = new ArrayList<>(); //共享类型
    private List<ShareBasicDataVO.TypeListBean> unitList = new ArrayList<>(); //单位类型
    private List<ShareBasicDataVO.TypeListBean> wayList = new ArrayList<>(); //共享方式
    private List<ShareBasicDataVO.TypeListBean> ModeList = new ArrayList<>(); //送取方式
    private OptionsPickerView pvType; //共享分类
    private ShareBasicDataVO baseVO;
    private SpinnerAdapter spinnerAdapter;

    private ShareFarmListVO vo;
    private List<ActivityTypeVO> typeList1 = new ArrayList<>();



    // 上传图片
    public static final int REQUEST_CODE_SELECT = 100; // 详情图上传
    public static final int REQUEST_CODE_SELECT_POSTER = 101; // 主图上传
    private ImagePicker imagePicker;
    private String[] imgs = {"", "", "", "", "", "", "", ""};
    private SimpleDraweeView[] sdvArray = new SimpleDraweeView[8];
    private int select = -1; // -1为上传主图  其他上传详情图

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_add_share_farm);
        setTitle("发布共享");
    }

    @Override
    public void initMember() {


        initData();


        //获取分类及基础数据
        BusinessShareFarm.getPublishShareBasicData1(this, mHandler);

        pvType = new OptionsPickerView.Builder(AddShareFarmActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                if (typeList != null && typeList.size() > 0) {
                    String select = typeList.get(options1).getPickerViewText();
                    tvType.setText(select);
                    type = typeList.get(options1).getCode();
                }
            }
        })
                .setSubCalSize(16)//确定取消按钮大小
                .setLineSpacingMultiplier(1.5f) //设置两横线之间的间隔倍数（范围：1.2 - 2.0倍 文字高度）
                .setDividerColor(getResources().getColor(R.color.colorFontLightGrayB))//设置分割线的颜色
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(18)//设置滚轮文字大小
                .setSelectOptions(0)  //设置默认选中项
                .build();

        pvType.setPicker(emptyList);

//        spinnerAdapter = new SpinnerAdapter(this, unitList);
//        spType.setAdapter(spinnerAdapter);


        if (getIntent().hasExtra("ShareFarmListVO")) {
            vo = (ShareFarmListVO) getIntent().getSerializableExtra("ShareFarmListVO");
            setView();
        }
    }

    // 编辑按钮跳转过来 则回填数据
    private void setView() {
        /*// 共享ID
        productId = vo.getId();
        // 共享名称
        name = vo.getName();
        etName.setText(name);
        // 共享分类
        type = vo.getType().getCode();
        tvType.setText(vo.getType().getName());
        // 共享/出售code
        shareType = "";
        for (int i = 0; i < vo.getShareType().size(); i++) {
            shareType += vo.getShareType().get(i).getCode() + ",";
        }
        shareType = shareType.substring(0, shareType.length() - 1);
        if (vo.getShareType().size() == 2) {
            cbShare.setChecked(true);
            cbSell.setChecked(true);
            layoutSell.setVisibility(View.VISIBLE);
            layoutShare.setVisibility(View.VISIBLE);
            layoutCash.setVisibility(View.VISIBLE);

            // 出售价格
            sharePrice = String.valueOf(vo.getPrice());

            // 共享价格
            for (int i = 0; i < vo.getAgricultureProductPriceList().size(); i++) {
                if ("共享".equals(vo.getAgricultureProductPriceList().get(i).getAttrName())) {
                    salePrice = String.valueOf(vo.getAgricultureProductPriceList().get(i).getPrice());
                }

            }

            // 共享单位code
            if (vo.getUnit() != null)
                unit = vo.getUnit().getCode();

            // 保证金
            securityDeposit = String.valueOf(vo.getEnsureMoney());

            //共享数量
            productNum = String.valueOf(vo.getProductNum());

            etSellPrice.setText(sharePrice);
            etSharePrice.setText(salePrice);
            etCashEposit.setText(securityDeposit);

        } else {
            if (shareType.equals("30021")) { // 只有共享
                cbShare.setChecked(true);
                cbSell.setChecked(false);
                layoutSell.setVisibility(View.GONE);
                layoutShare.setVisibility(View.VISIBLE);
                layoutCash.setVisibility(View.VISIBLE);
                // 共享价格
                for (int i = 0; i < vo.getAgricultureProductPriceList().size(); i++) {
                    if ("共享".equals(vo.getAgricultureProductPriceList().get(i).getAttrName())) {
                        salePrice = String.valueOf(vo.getAgricultureProductPriceList().get(i).getPrice());
                    }

                }

                // 共享单位code
                if (vo.getUnit() != null)
                    unit = vo.getUnit().getCode();

                // 保证金
                securityDeposit = String.valueOf(vo.getEnsureMoney());

                //共享数量
                productNum = String.valueOf(vo.getProductNum());


                etSharePrice.setText(salePrice);
                etCashEposit.setText(securityDeposit);

            } else { // 只有出售
                cbShare.setChecked(false);
                cbSell.setChecked(true);
                layoutSell.setVisibility(View.VISIBLE);
                layoutShare.setVisibility(View.GONE);
                layoutCash.setVisibility(View.GONE);
                // 出售价格
                sharePrice = String.valueOf(vo.getPrice());
                etSellPrice.setText(sharePrice);
            }
        }

        etShareNum.setText(productNum);


        // 取送方式
        deliveryMode = "";
        for (int i = 0; i < vo.getDeliveryMode().size(); i++) {
            deliveryMode += vo.getDeliveryMode().get(i).getCode() + ",";
        }
        deliveryMode = deliveryMode.substring(0, deliveryMode.length() - 1);

        if (vo.getDeliveryMode().size() == 2) {
            cbExpress.setChecked(true);
            cbTake.setChecked(true);
            // 自提地址
            retentionAddress = vo.getRetentionAddress() + "";
            etTakeAddress.setText(retentionAddress);
        } else {
            if (deliveryMode.equals("702")) { // 只有快递
                cbExpress.setChecked(true);
                cbTake.setChecked(false);
                layoutAddress.setVisibility(View.GONE);
            } else {// 只有自提
                cbExpress.setChecked(false);
                cbTake.setChecked(true);
                layoutAddress.setVisibility(View.VISIBLE);
                // 自提地址
                retentionAddress = vo.getRetentionAddress() + "";
                etTakeAddress.setText(retentionAddress);
            }
        }


        // 是否包邮
        if (vo.getIsFreePostage() != null)
            isFreePostage = vo.getIsFreePostage();
        else
            isFreePostage = "1";

        // 运费价格 1-包邮  0-设置邮费
        if (isFreePostage.equals("0")) {
            postage = vo.getPostage();
            etPostPrice.setText(postage);
            rbPostageYes.setChecked(true);
            etPostPrice.setVisibility(View.VISIBLE);
        } else {
            rbPostageNo.setChecked(true);
            etPostPrice.setVisibility(View.INVISIBLE);
        }


        // 共享范围 1为不限制
        shareScopeFlag = String.valueOf(vo.getShareScopeFlag());
        if (shareScopeFlag.equals("1")) {
            rbScopeAll.setChecked(true);
            agricultureProductShareScopes = "";
            tvScope.setVisibility(View.INVISIBLE);
        } else {
            rbScopeSelect.setChecked(true);
            tvScope.setVisibility(View.VISIBLE);
            BusinessShareFarm.getShareProductDetail(this, productId, "", mHandler);
        }


        // 主图
        mainImage = vo.getProductImageList().get(0).getImage();
        FrescoUtil.showImageSmall(mainImage, ivImg);

        // 详情
        introduce = vo.getIntroduce();
        etInfo.setText(introduce);

        // 详情图

        for (int i = 0; i < vo.getAgricultureProductDescImageList().size(); i++) {
            String img = vo.getAgricultureProductDescImageList().get(i).getImage();
            imgs[i] = img;
            FrescoUtil.showImageSmall(img, sdvArray[i]);
        }*/

    }

    // 初始化数据
    private void initData() {
        selectImage();

        emptyList.add("");

        sdvArray[0] = sdvImg0;
        sdvArray[1] = sdvImg1;
        sdvArray[2] = sdvImg2;
        sdvArray[3] = sdvImg3;
        sdvArray[4] = sdvImg4;
        sdvArray[5] = sdvImg5;
        sdvArray[6] = sdvImg6;
        sdvArray[7] = sdvImg7;
    }

    @Override
    public void addListener() {

        // 单位类型
        spType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                unit = unitList.get(i).getCode();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


        // 是否包邮
        rgPostage.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (radioGroup.getCheckedRadioButtonId() == R.id.rb_postage_no) {
                    isFreePostage = "1";
                    etPostPrice.setVisibility(View.INVISIBLE);
                } else {
                    isFreePostage = "0";
                    etPostPrice.setVisibility(View.VISIBLE);
                }
            }
        });

        // 共享范围单选
        rgScope.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (radioGroup.getCheckedRadioButtonId() == R.id.rb_scope_all) { // 不限
                    shareScopeFlag = "1";
                    tvScope.setVisibility(View.INVISIBLE);
                } else {
                    shareScopeFlag = "0";
                    tvScope.setVisibility(View.VISIBLE);
                }
            }
        });


    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_GET_SHARE_BASIC_DATA_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_SHARE_BASIC_DATA_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_GET_SHARE_PRODUCT_DETAIL_SUCCESS: // 共享商品详情
                      /*  ShareFarmListVO vo = FastJsonUtil.getBean(msg.obj.toString(), "product", ShareFarmListVO.class);
                        if (vo != null && vo.getShareScopeJson() != null)
                            agricultureProductShareScopes = vo.getShareScopeJson();
                        break;*/
                    case MSG.MSG_PUBLISH_SHARE_PRODUCT_SUCCESS:
                        showToast("发布成功");
                        setResult(RESULT_OK);
                        finish();
                        break;
                    case MSG.MSG_PUBLISH_SHARE_PRODUCT_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_FILE_UPLOAD_SUCCESS: // 上传图片
                        dismissDialog();
                        FileVO fileVO = FastJsonUtil.getBean(msg.obj.toString(), "fileStorage", FileVO.class);
                        if (fileVO != null) {
                            if (select == -1) {
                                mainImage = fileVO.getName();
                                FrescoUtil.showImageSmall(mainImage, ivImg);
                            } else {
                                imgs[select] = fileVO.getName();
                                setImageView(fileVO.getName());
                            }
                        }
                        break;
                    case MSG.MSG_FILE_UPLOAD_FIELD:
                        dismissDialog();
                        showToast(msg.obj.toString() + "");
                        break;
                }
            }
        };
    }

    private void setImageView(String fileName) {
        FrescoUtil.showImageSmall(fileName, sdvArray[select]);
    }

    // 选择图片
    private void selectImage(int i) {
        select = i;
        if (StringUtil.isStrEmpty(imgs[select])) {
            Intent intent = new Intent(AddShareFarmActivity.this, ImageGridActivity.class);
            startActivityForResult(intent, REQUEST_CODE_SELECT);
        } else {
            new MaterialDialog.Builder(AddShareFarmActivity.this)
                    .title("提示")
                    .content("是否删除这张照片？")
                    .positiveText("删除")
                    .negativeText("取消")
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            imgs[select] = "";
                            Uri uri = Uri.parse("res://com.digitalchina.bigdata/" + R.drawable.selector_image_add);
                            sdvArray[select].setImageURI(uri);
                        }
                    })
                    .show();
        }

    }


    /**
     * 跳转至自定义相册
     */
    private void selectImage() {
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());   //设置图片加载器
        imagePicker.setShowCamera(true);  //显示拍照按钮
        imagePicker.setMultiMode(false);    //是否多选模式
        imagePicker.setCrop(false);        //允许裁剪（单选才有效）
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            //添加图片返回
            if (data != null && requestCode == REQUEST_CODE_SELECT) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                if (images != null && images.size() > 0) {
                    showDialog("正在上传图片...");
                    BusinessFile.imageCompressAndUpload(this, images.get(0).path, mHandler);
                }
            }
        } else if (resultCode == RESULT_OK) {
            if (data != null && requestCode == 99) {
                List<ShareSelectCityVO> pList = (List<ShareSelectCityVO>) data.getSerializableExtra("list");
                agricultureProductShareScopes = JSON.toJSONString(pList, true);
            }
        }
    }

    // 获取上传所有图片拼接字符串
    public String getImages() {

        String s = "";
        for (String img : imgs) {
            if (!StringUtil.isStrEmpty(img))
                s += img + ",";
        }

        if (!StringUtil.isStrEmpty(s))
            s = s.substring(0, s.length() - 1);

        return s;
    }

    @Override
    public void callBack(Object object) {

//        baseVO = FastJsonUtil.getBean(object.toString(), "body", "", ShareBasicDataVO.class);
//
//        if (baseVO == null)
//            return;
//
//        typeList = baseVO.getShareTypeList();
//        unitList = baseVO.getUnitTypeList();
//        wayList = baseVO.getShareTypeList();
//        ModeList = baseVO.getShareTypeList();

//        pvType.setPicker(typeList);
//        spinnerAdapter.setData(unitList);


        typeList1 = FastJsonUtil.getListBeanRow(object.toString(), "options", ActivityTypeVO.class);


        for (int i = 0; i < unitList.size(); i++) {
            if (unit.equals(unitList.get(i).getCode())) {
                spType.setSelection(i, true);
            }
        }
    }


    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_type, R.id.iv_img, R.id.sdv_img0, R.id.sdv_img1, R.id.sdv_img2, R.id.sdv_img3, R.id.sdv_img4, R.id.sdv_img5, R.id.sdv_img6,
            R.id.sdv_img7, R.id.btn_save, R.id.tv_scope, R.id.cb_share, R.id.cb_sell, R.id.cb_take})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_type:
                pvType.show();
                break;
            case R.id.iv_img:
                select = -1;
                Intent intent = new Intent(AddShareFarmActivity.this, ImageGridActivity.class);
                startActivityForResult(intent, REQUEST_CODE_SELECT);
                break;
            case R.id.cb_share: // 共享 cb
                if (cbShare.isChecked()) {
                    layoutShare.setVisibility(View.VISIBLE);
                    layoutCash.setVisibility(View.VISIBLE);
                } else {
                    layoutShare.setVisibility(View.GONE);
                    layoutCash.setVisibility(View.GONE);
                }
                break;
            case R.id.cb_sell: // 出售 cb
                if (cbSell.isChecked()) {
                    layoutSell.setVisibility(View.VISIBLE);
                } else {
                    layoutSell.setVisibility(View.GONE);
                }
                break;
            case R.id.cb_take:
                if (cbTake.isChecked()) {
                    layoutAddress.setVisibility(View.VISIBLE);
                } else {
                    layoutAddress.setVisibility(View.GONE);
                }
                break;
            case R.id.sdv_img0:
                selectImage(0);
                break;
            case R.id.sdv_img1:
                selectImage(1);
                break;
            case R.id.sdv_img2:
                selectImage(2);
                break;
            case R.id.sdv_img3:
                selectImage(3);
                break;
            case R.id.sdv_img4:
                selectImage(4);
                break;
            case R.id.sdv_img5:
                selectImage(5);
                break;
            case R.id.sdv_img6:
                selectImage(6);
                break;
            case R.id.sdv_img7:
                selectImage(7);
                break;
            case R.id.btn_save:
                save();
                break;
            case R.id.tv_scope: // 共享范围
                Map<String, Object> map = new HashMap<>();
                map.put("productId", productId);
                map.put("isDetail", false);
                GotoUtil.gotoForResultActivity(AddShareFarmActivity.this, ShareSelectCityActivity.class, 99, "map", map);
                break;
        }
    }

    /**
     * String productId                     共享产品id
     * String name                          共享名称
     * String type                          共享分类code
     * String shareType                     共享方式 30021,30022
     * String sharePrice                    共享价格
     * String salePrice                     出售价格
     * String unit                          共享价格单位code
     * String securityDeposit               保证金
     * String productNum                    共享数量
     * String deliveryMode                  取送方式 702,703
     * String retentionAddress              自提地址
     * String isFreePostage                 运费设置 1-包邮  0-设置邮费
     * String postage                       邮费
     * String shareScopeFlag                共享范围类型 1-不限 0-限制范围
     * String agricultureProductShareScopes 范围类型
     * String mainImage                     主图
     * String introduce                     详情
     * String pictures                      详情图片(以逗号隔开)
     */
    private void save() {

        name = etName.getText().toString();
        salePrice = etSellPrice.getText().toString();
        sharePrice = etSharePrice.getText().toString();
        securityDeposit = etCashEposit.getText().toString();
        productNum = etShareNum.getText().toString();
        retentionAddress = etTakeAddress.getText().toString();
        postage = etPostPrice.getText().toString();
        introduce = etInfo.getText().toString();

        if (StringUtil.isStrEmpty(name)) {
            showToast("请输入共享名称");
            return;
        }
        if (StringUtil.isStrEmpty(type)) {
            showToast("请选择共享分类");
            return;
        }

        if (!cbShare.isChecked() && !cbSell.isChecked()) {
            showToast("请选择共享方式");
            return;
        }

        if (cbShare.isChecked() && cbSell.isChecked()) {
            shareType = "30021,30022";
            if (StringUtil.isStrEmpty(sharePrice)) {
                showToast("请填写共享价格");
                return;
            }
            if (StringUtil.isStrEmpty(securityDeposit)) {
                showToast("请填写保证金");
                return;
            }
            if (StringUtil.isStrEmpty(salePrice)) {
                showToast("请填写出售价格");
                return;
            }
        } else {
            if (cbShare.isChecked()) {

                shareType = "30021";
                if (StringUtil.isStrEmpty(sharePrice)) {
                    showToast("请填写共享价格");
                    return;
                }
                if (StringUtil.isStrEmpty(securityDeposit)) {
                    showToast("请填写保证金");
                    return;
                }
            }else{
                shareType = "30022";
                if (StringUtil.isStrEmpty(salePrice)) {
                    showToast("请填写出售价格");
                    return;
                }
            }
        }

        if (!cbExpress.isChecked() && !cbTake.isChecked()) {
            showToast("请选择取送方式");
            return;
        }

        if (cbExpress.isChecked() && cbTake.isChecked()) {
            deliveryMode = "702,703";
            if (StringUtil.isStrEmpty(retentionAddress)) {
                showToast("请填写自提地址");
                return;
            }
        }else{
            if(cbExpress.isChecked()){
                deliveryMode = "702";
            }else{
                deliveryMode = "703";
                if (StringUtil.isStrEmpty(retentionAddress)) {
                    showToast("请填写自提地址");
                    return;
                }
            }
        }



        if (StringUtil.isStrEmpty(productNum)) {
            showToast("请填写共享数量");
            return;
        }


        if (isFreePostage.equals("0")) {
            if (StringUtil.isStrEmpty(postage)) {
                showToast("请填写运费");
                return;
            }
        }

        if (shareScopeFlag.equals("0")) {
            if (StringUtil.isStrEmpty(agricultureProductShareScopes)) {
                showToast("请选择共享范围");
                return;
            }
        }

        if (StringUtil.isStrEmpty(mainImage)) {
            showToast("请上传共享主图");
            return;
        }

        if (StringUtil.isStrEmpty(introduce)) {
            showToast("请输入简要描述");
            return;
        }

        pictures = getImages();


        BusinessShareFarm.publishShareProductMobile(this, productId, name, type, shareType, sharePrice, salePrice, unit, securityDeposit, productNum, deliveryMode, retentionAddress, isFreePostage,
                postage, shareScopeFlag, agricultureProductShareScopes, mainImage, introduce, pictures, mHandler);
    }

    public class SpinnerAdapter extends BaseAdapter {
        private List<ShareBasicDataVO.TypeListBean> mList;
        private Context mContext;

        public SpinnerAdapter(Context pContext, List<ShareBasicDataVO.TypeListBean> pList) {
            this.mContext = pContext;
            this.mList = pList;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public void setData(List<ShareBasicDataVO.TypeListBean> pList) {
            this.mList = pList;
            notifyDataSetChanged();
        }

        /**
         * 下面是重要代码
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater _LayoutInflater = LayoutInflater.from(mContext);
            convertView = _LayoutInflater.inflate(R.layout.item_spinner, null);
            if (convertView != null) {
                TextView tvName = convertView.findViewById(R.id.tv_name);
                tvName.setText(mList.get(position).getName());
            }
            return convertView;
        }
    }
}
